<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="商品规格/商品参数管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">
            <el-form-item label="商品类型" prop="goodsTypeId">
                <el-select v-model="formData.goodsTypeId" filterable placeholder="请选择" style="width:200px;">
                    <el-option
                        v-for="item in goodsTypeAllList"
                        :key="item.id"
                        :label="item.goodsTypeName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="规格/参数" prop="goodsTypeId">
                <el-radio-group v-model="formData.specType">
                    <el-radio label="1" size="large">规格</el-radio>
                    <el-radio label="2" size="large">参数</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="品牌" prop="brandId" size="mini">
                <el-select v-model="formData.brandId" placeholder="请选择" clearable>
                    <el-option
                        v-for="item in brandList"
                        :key="item.id"
                        :label="item.brandName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="名称" prop="specName">
                <el-input v-model="formData.specName"/>
            </el-form-item>

            <el-form-item label="检索" prop="filterCondition">
                <el-switch v-model="formData.filterCondition"/>
                <div class="form-tips">查询商品时是否为检索条件</div>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="formData.sort" :min="1" :max="999999"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {SpecApi} from '@/api/mall/spec'
import {BrandApi} from '@/api/mall/brand'
import {GoodsTypeApi} from '@/api/mall/goodsType'
import {Common} from '@/utils/common'

export default {
    name: 'MallSpecUpdate',
    components: {},
    data() {
        return {
            formData: {
                specId: 0,
                goodsTypeId: '',
                brandId: '',
                specName: '',
                specType: '1',
                filterCondition: false,
                sort: ''
            },
            rules: {
                goodsTypeId: [
                    {required: true, message: '请选择商品类型', trigger: 'blur'}
                ],
                specName: [
                    {required: true, message: '请输入规格名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            goodsTypeAllList: [],
            brandList: [] // 品牌

        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            const that = this
            this.dialogVisible = true

            GoodsTypeApi.getAllList().then((response) => {
                that.goodsTypeAllList = response
            }).catch((err) => {
            })

            BrandApi.getAllList().then((response) => {
                this.brandList = response
            })

            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }

                if (data.specId > 0) {
                    data.specType = data.specType.toString()
                    data.brandId = data.brandId.toString()
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.specId = 0
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.specId > 0) {
                        SpecApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        SpecApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }

    }
}
</script>
